import 'package:flutter/material.dart';
import 'news_details/news_details.dart';
import 'dart:async';
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';

Dio dio = new Dio();

class News extends StatefulWidget {
  News({Key key}) : super(key: key);

  _NewsState createState() => _NewsState();
}

class _NewsState extends State<News> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Image.asset(
          'images/title.jpg',
          height: 25,
        ),
      ),
      body: Cardp(),
    );
  }
}

class Cardp extends StatefulWidget {
  Cardp({Key key}) : super(key: key);

  _CardpState createState() => _CardpState();
}

class _CardpState extends State<Cardp> {
  var cang;
  var _res;
  var user_id;
  var xuehao;

  void message() async {
    Response response = await dio.get(
        'http://xssw.gdit.edu.cn/sizheng/api/v1/message?sort=-createdAt');
    _res = response.data;
    setState(() {});
  }

void shoucang() async{
   Response response;
    if(xuehao.length<10){
       response =
        await dio.get('http://192.168.1.129:3698/api/v1/collect');  //id 传进来的 //student/id  teacher/id
    }else{
       response =
        await dio.get('http://xssw.gdit.edu.cn/sizheng/api/v1/student/${user_id}');  //id 传进来的 //student/id  teacher/id
    }
        setState(() {});
}

getName() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
      user_id = sharedPreferences.getString('user_id');
      xuehao = sharedPreferences.getString('number');
      shoucang();     // 初始化根据ID拉取收藏数据
      setState(() {});
  }

 @override
  void initState() {
    super.initState();
    message();  // 初始化拉取数据
    getName();
  }

  ListView getListView() => new ListView.builder(
      itemCount: _res.length,
      itemBuilder: (BuildContext context, int position) {
        return Stack(
          children: <Widget>[
            Container(
              child: Column(
                children: <Widget>[
                  InkWell(
                    child: Column(
                      children: <Widget>[
                        SizedBox(height: 15,),
                        ListTile(
                      leading: Image.asset('images/email.png'),
                      title: Text(
                        '${_res[position]['title']}',
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                      subtitle: Text(
                        '${_res[position]['contain']}',
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      ),
                      trailing: Text(
                          '${_res[position]['createdAt'].substring(0, 10)}'),
                    ),
                    SizedBox(height: 15,),
                      ],
                    ),
                    onTap: () {
                      Navigator.of(context).push(MaterialPageRoute(
                          builder: (context) => NewsDetails(
                              title: _res[position]['title'],
                              contain: _res[position]['contain'],
                              from: _res[position]['from'],
                              createdAt: _res[position]['createdAt'])));
                    },
                  ),
                   Row(
                    children: <Widget>[
                      SizedBox(
                        width: 90,
                      ),
                      Container(
                        width: 270,
                        height: 0.5,
                        decoration: BoxDecoration(
                            border: Border.all(
                                color: Color.fromRGBO(220, 220, 220, 1))),
                      )
                    ],
                  ),
                ],
              ),
            )
          ],
        );
      });

  @override
  Widget build(BuildContext context) {
    return _res == null
        ? Center(
            child: Image.asset('images/nomessage.png'),
          )
        : getListView();
  }
}
